<?php
namespace app\app\controller\v1;

use think\Db;
use think\Request;
use app\app\util\OnlineUtils;

class CollectController extends BaseController{

    /**
     * 显示资源列表
     * 收藏列表(收藏我的)
     * @return \think\Response
     */
    public function index()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        if(input('get.id') && input('get.id') >0){
            $page = config('PAGE_NUM').input('get.id');
        }else{
            $page = 0;
        }
        $collect = Db::query('select a.id,a.user_id,b.username,b.portrait,c.id,c.name,c.position,c.company_name from hmp_collect a left join hmp_users b on a.user_id=b.id left join hmp_card c on a.user_id=c.user_id where a.collected_user_id='.$user_id.' and b.status=1 order by a.id desc limit '.$page.',10');
        if($collect){

        }else{
            $collect = [];
        }
        return hmp(200,'success',['data'=>$collect]);
    }

    /**
     * [getMyCollectList 我的收藏]
     * @return [type] [description]
     */
    public function getMyCollectList()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        if(input('get.id') && input('get.id') >0){
            $page = config('PAGE_NUM').input('get.id');
        }else{
            $page = 0;
        }
        //搜索
        if(input('keyword') && input('keyword') !=''){
            $where = " and c.keyword like '%".input('keyword')."%'"; 
        }else{
			$where = " ";
		}
        //数据
        $collect = Db::query('select a.id,a.collected_user_id as user_id,b.username,b.portrait,c.id,c.name,c.position,c.company_name,b.portrait,b.phone from hmp_collect a left join hmp_users b on a.collected_user_id=b.id left join hmp_card c on a.card_id=c.id where a.user_id='.$user_id.' and b.status=1 '.$where.' order by a.id desc limit '.$page.',10');
        if($collect){
            //查询优惠券
            foreach($collect as $k => $v){
                $coupon = Db::name('coupon')->where('user_id','eq',$v['user_id'])->where('status','neq',0)->select();
                $collect[$k]['coupon'] = $coupon;
            }
        }else{
            $collect = [];
        }             
        return hmp(200,'success',['data'=>$collect]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     * 点赞
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $id = intval(input('post.id'));
        $card = Db::name('card')->where('id','eq',$id)->find();
        if($card['user_id'] == $user_id) return hmp(402,'无法收藏自己的名片');
        $has = Db::name('collect')->where('user_id','eq',$user_id)->where('collected_user_id','eq',$card['user_id'])->find();
        if($has){
            return hmp(402,'您已经收藏该名片');
        }
        $collect = array('user_id'=>$user_id,'collected_user_id'=>$card['user_id'],'card_id'=>$id);
        $result = Db::name('collect')->insertGetId($collect);
        if($result){
            Db::query('update hmp_card set collect = collect + 1 where id ='.$id);
            return hmp(200,'success');
        }else{
            return hmp(402,'fail');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update($id)
    {

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $id = intval(input('id'));
        $collect = Db::name('colelct')->where('id','eq',$id)->find();
        if($collect){
            Db::query('update hmp_users set collect = collect - 1 where id ='.$collect['collected_user_id']);
            Db::name('collect')->where('id','eq',$id)->delete();
            return hmp(200,'success');
        }else{
            return hmp(402,'fail');
        }
    }
	
    public function cancelCollect()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $id = intval(input('id'));
        $collect = Db::name('collect')->where('card_id','eq',$id)->where('user_id','eq',$user_id)->find();
        if($collect){
            //删除记录
            Db::name('collect')->where('id','eq',$collect['id'])->delete();
            //次数减少
            Db::query('update hmp_card set collect = collect - 1 where id ='.$id);
            return hmp(200,'success');
        }else{
            return hmp(402,'fail');
        }        
    }	
	
}